package com.czh.subject.common.entity;

import lombok.Getter;
import lombok.Setter;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 分页
 *
 * @author caozaihui
 * @version 1.0
 * @description: TODO tip
 * @since 2024/8/2 下午 6:35
 */
@Getter
public class PageResult<T> {
    @Setter
    private Integer pageNo;
    @Setter
    private Integer pageSize;
    //总数据条数
    private Integer total = 0;
    //总页数
    private Integer totalPages = 0;

    private List<T> result = Collections.emptyList();
    //当前页第一条数据索引
    private Integer start = 1;
    //当前页最后一条数据索引
    private Integer end = 0;

    public void setResult(List<T> result) {
        this.result = result;
        if (!result.isEmpty()) {
            setTotal(result.size());
        }
    }

    public void setTotal(Integer total) {
        this.total = total;
        if (Objects.nonNull(this.pageNo) && Objects.nonNull(this.pageSize)) {
            this.totalPages =this.pageSize > 0 ? total / this.pageSize + (total % this.pageSize == 0 ? 0 : 1) : 0;
            this.start = this.pageSize >= 0 ? (this.pageNo - 1) * this.pageSize + 1 : 0;
            this.end = this.start + this.pageSize;
        } else {
            this.start = this.end = this.pageNo = this.pageSize = 0;
        }
    }
}
